using Produce.Domain.Entities;
using SqlSugar;
using System.Collections.Generic;

namespace Produce.Infrastructure.Repositories
{
    public class ProduceModelRepository<T> : IProduceModelRepository<T> where T : BaseEntity, new()
    {
        private readonly ISqlSugarClient _db;
        public ProduceModelRepository(ISqlSugarClient db)
        {
            _db = db;
        }


        /// <summary>
        /// ����ģ�͵����ݿ�
        /// </summary>
        /// <param name="model">Ҫ�����ģ��</param>
        /// <returns>ִ�в������Ӱ�������</returns>
        public int Create(T model)
        {
            if (model == null)
                throw new ArgumentNullException(nameof(model));

            try
            {
                return _db.Insertable(model).ExecuteCommand();
            }
            catch (Exception ex)
            {
                // ��¼��־�������׳����������Ŀ�ṹ��ͳһ�쳣����
                Console.WriteLine($"Error creating model: {ex.Message}");
                throw;
            }
        }

        /// <summary>
        /// ����ID��ȡģ��
        /// </summary>
        /// <param name="id">ģ�͵�Ψһ��ʶ��</param>
        /// <returns>��Ӧ��ģ��ʵ�������δ�ҵ��򷵻�null</returns>
        public T GetModel(long id)
        {
            if (id <= 0)
                throw new ArgumentOutOfRangeException(nameof(id), "ID ������� 0");

            try
            {
                return _db.Queryable<T>().InSingle(id);
            }
            catch (Exception ex)
            {
                Console.WriteLine($"Error fetching model with ID {id}: {ex.Message}");
                throw;
            }
        }

        /// <summary>
        /// 获取所有模型的列表
        /// </summary>
        /// <returns>包含所有模型实例的列表</returns>
        public ISugarQueryable<T> GetValue()
        {
            try
            {
                if (_db == null)
                {
                    throw new InvalidOperationException("数据库连接未初始化，请检查依赖注入配置");
                }

                // 警告：大数据量下可能影响性能，建议后续支持分页
                return _db.Queryable<T>();
            }
            catch (InvalidOperationException ex)
            {
                Console.WriteLine($"数据库连接错误: {ex.Message}");
                throw;
            }
            catch (Exception ex)
            {
                Console.WriteLine($"查询数据时发生错误: {ex.Message}");
                if (ex.InnerException != null)
                {
                    Console.WriteLine($"内部异常: {ex.InnerException.Message}");
                }
                throw;
            }
        }

        /// <summary>
        /// ����ģ�͵����ݿ�
        /// </summary>
        /// <param name="model">Ҫ���µ�ģ��</param>
        /// <returns>ִ�и��²���Ӱ�������</returns>
        public int Update(T model)
        {
            if (model == null)
                throw new ArgumentNullException(nameof(model));

            try
            {
                return _db.Updateable(model).ExecuteCommand();
            }
            catch (Exception ex)
            {
                Console.WriteLine($"Error updating model: {ex.Message}");
                throw;
            }
        }
    }
}